package com.gy.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

public class PipeTest {

    public static void main(String[] args) throws IOException {
        //获取管道
        Pipe pipe = Pipe.open();
        //创建缓冲区对象
        ByteBuffer buf = ByteBuffer.allocate(1024);

        //获取sink通道
        Pipe.SinkChannel sinkChannel = pipe.sink();

        //将数据写入到sinkChannel
        buf.put("测试数据".getBytes());
        buf.flip();
        sinkChannel.write(buf);

        //读取sourceChannel中的数据放入到缓冲区中
        buf.flip();
        Pipe.SourceChannel sourceChannel = pipe.source();
        int len = sourceChannel.read(buf);
        System.out.println(new String(buf.array(), 0, len));

        sourceChannel.close();
        sinkChannel.close();
    }
}
